<%@ page pageEncoding="utf-8"%>
<%@ taglib prefix="t" uri="http://tiles.apache.org/tags-tiles" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="com.warm.doctor.utils.WarmmedConfig" %>
<!doctype html>
<%
    request.setAttribute("path", request.getContextPath());
%>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <title>咨询医生</title>
    <meta name="keywords" content="咨询医生">
    <meta name="description" content="咨询医生">
    <meta name="format-detection" content="telephone=no">
    <meta content="email=no" name="format-detection">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta content="black" name="apple-mobile-web-app-status-bar-style">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="msapplication-tap-highlight" content="no">
    <link rel="stylesheet" href="<%= WarmmedConfig.static_server_url %>/warm/css/min/consultDoctor.build.css"/>
    <link rel="stylesheet" href="//at.alicdn.com/t/font_1438680329_5325851.css"/>
</head>

<body data-hour="${result.consultingTime}">
<nav id="nav">
        <a href="javascript:void(0)" class="nav_home" data-tab="homeTab">医生主页</a>
        <a href="javascript:void(0)" class="nav_consult on" data-tab="consultTab">咨询服务</a>
</nav>

<div class="consultTab">
    <div class="consult" >
    </div>
    <footer>
        <form>
            <span style="margin-left: 0;"><input type="text" name="send" id="send" placeholder="输入你想回复的内容" /></span>
            <em><input type="button" value="发送" id="sendBtn"/></em>
        </form>
    </footer>
</div>
<div class="hide homeTab">
    <header style="top: 50px">
        <img src="<%= WarmmedConfig.static_server_url %>/warm/img/doctor_banner.jpg">
        <div class="doctor_pic">
            <c:if test="${result.avatar ==null}">
                <img src="<%= WarmmedConfig.static_server_url %>/warm/img/doctor_pic.png">
            </c:if>
            <c:if test="${result.avatar != null}">
                <img src="${result.avatar}">
            </c:if>
            <p class="col_6"><strong class="ft16 bf">${result.username}</strong> ${result.title}<br>${result.department}｜${result.hospital}</p>
        </div>
    </header>
    <div class="doctor_time" style="margin-top: 55px">
        <span>门诊时间</span>
        <em class="col_k6" id="hour"></em>
    </div>
</div>
<script src="http://res.websdk.rongcloud.cn/RongIMClient-0.9.11.min.js"></script>
<script src="${path}/media/js/jquery-1.10.1.min.js" type="text/javascript"></script>
<script type="text/javascript">

    RongIMClient.init("<%= WarmmedConfig.rong_app_key %>");
    var token = "${serverToken}";

    RongIMClient.connect(token,{
        onSuccess: function (userId) {
            // 此处处理连接成功。
            console.log("Login successfully." + userId);
        },
        onError: function (errorCode) {
            // 此处处理连接错误。
            var info = '';
            switch (errorCode) {
                case RongIMClient.callback.ErrorCode.TIMEOUT:
                    info = '超时';
                    break;
                case RongIMClient.callback.ErrorCode.UNKNOWN_ERROR:
                    info = '未知错误';
                    break;
                case RongIMClient.ConnectErrorStatus.UNACCEPTABLE_PROTOCOL_VERSION:
                    info = '不可接受的协议版本';
                    break;
                case RongIMClient.ConnectErrorStatus.IDENTIFIER_REJECTED:
                    info = 'appkey不正确';
                    break;
                case RongIMClient.ConnectErrorStatus.SERVER_UNAVAILABLE:
                    info = '服务器不可用';
                    break;
                case RongIMClient.ConnectErrorStatus.TOKEN_INCORRECT:
                    info = 'token无效';
                    break;
                case RongIMClient.ConnectErrorStatus.NOT_AUTHORIZED:
                    info = '未认证';
                    break;
                case RongIMClient.ConnectErrorStatus.REDIRECT:
                    info = '重新获取导航';
                    break;
                case RongIMClient.ConnectErrorStatus.PACKAGE_ERROR:
                    info = '包名错误';
                    break;
                case RongIMClient.ConnectErrorStatus.APP_BLOCK_OR_DELETE:
                    info = '应用已被封禁或已被删除';
                    break;
                case RongIMClient.ConnectErrorStatus.BLOCK:
                    info = '用户被封禁';
                    break;
                case RongIMClient.ConnectErrorStatus.TOKEN_EXPIRE:
                    info = 'token已过期';
                    break;
                case RongIMClient.ConnectErrorStatus.DEVICE_ERROR:
                    info = '设备号错误';
                    break;
            }
            console.log("失败:" + info);
        }
    });

    RongIMClient.setConnectionStatusListener({
        onChanged: function (status) {
            switch (status) {
                //链接成功
                case RongIMClient.ConnectionStatus.CONNECTED:
                    console.log('链接成功');
                    break;
                //正在链接
                case RongIMClient.ConnectionStatus.CONNECTING:
                    console.log('正在链接');
                    break;
                //重新链接
                case RongIMClient.ConnectionStatus.RECONNECT:
                    console.log('重新链接');
                    break;
                //其他设备登陆
                case RongIMClient.ConnectionStatus.OTHER_DEVICE_LOGIN:
                //连接关闭
                case RongIMClient.ConnectionStatus.CLOSURE:
                //未知错误
                case RongIMClient.ConnectionStatus.UNKNOWN_ERROR:
                //登出
                case RongIMClient.ConnectionStatus.LOGOUT:
                //用户已被封禁
                case RongIMClient.ConnectionStatus.BLOCK:
                    break;
            }
        }});

    // 消息监听器
    RongIMClient.getInstance().setOnReceiveMessageListener({
        // 接收到的消息
        onReceived: function (message) {

            // 判断消息类型
            switch(message.getMessageType()){
                case RongIMClient.MessageType.TextMessage:
                    var html = '<div class="consult_you">'+
                            ' <i></i>'+
                            ' <em>'+message.getContent()+'</em>'+
                            ' <span><img src="<%= WarmmedConfig.static_server_url %>/warm/img/doctor_pic.png"></span>'+
                            ' </div>';
                    $(".consult").append(html);
                    break;
                case RongIMClient.MessageType.ImageMessage:
                    // do something...
                    break;
                case RongIMClient.MessageType.VoiceMessage:
                    // do something...
                    break;
                case RongIMClient.MessageType.RichContentMessage:
                    // do something...
                    break;
                case RongIMClient.MessageType.LocationMessage:
                    // do something...
                    break;
                case RongIMClient.MessageType.DiscussionNotificationMessage:
                    // do something...
                    break;
                case RongIMClient.MessageType.InformationNotificationMessage:
                    // do something...
                    break;
                case RongIMClient.MessageType.ContactNotificationMessage:
                    // do something...
                    break;
                case RongIMClient.MessageType.ProfileNotificationMessage:
                    // do something...
                    break;
                case RongIMClient.MessageType.CommandNotificationMessage:
                    // do something...
                    break;
                case RongIMClient.MessageType.UnknownMessage:
                    // do something...
                    break;
                default:
                // 自定义消息
                // do something...
            }
        }
    })

    $("#sendBtn").click(function () {

        var msg = RongIMClient.TextMessage.obtain($("#send").val());
        var html = '<div class="consult_me">'+
                   ' <i></i>'+
                   ' <em>'+$("#send").val()+'</em>'+
                   ' <span><img src="<%= WarmmedConfig.static_server_url %>/warm/img/doctor_pic.png"></span>'+
                   ' </div>';

        var conversationtype = RongIMClient.ConversationType.PRIVATE; // 私聊
        var targetId = "${result.id}"; // 目标 Id
        RongIMClient.getInstance().sendMessage(conversationtype, targetId, msg, null, {
                    // 发送消息成功
                    onSuccess: function (data) {
                        $(".consult").append(html);
                        $("#send").val("");
                    },
                    onError: function (errorCode) {
                        var info = '';
                        switch (errorCode) {
                            case RongIMClient.callback.ErrorCode.TIMEOUT:
                                info = '超时';

                                break;
                            case RongIMClient.callback.ErrorCode.UNKNOWN_ERROR:
                                info = '未知错误';
                                break;
                            case RongIMClient.SendErrorStatus.REJECTED_BY_BLACKLIST:
                                info = '在黑名单中，无法向对方发送消息';
                                break;
                            case RongIMClient.SendErrorStatus.NOT_IN_DISCUSSION:
                                info = '不在讨论组中';
                                break;
                            case RongIMClient.SendErrorStatus.NOT_IN_GROUP:
                                info = '不在群组中';
                                break;
                            case RongIMClient.SendErrorStatus.NOT_IN_CHATROOM:
                                info = '不在聊天室中';
                                break;
                            default :
                                info = x;
                                break;
                        }
                        console.alert('发送失败:' + info);
                    }
                }
        );
    })
    /*RongIMClient.getInstance().syncConversationList({
        onSuccess:function(){
            setTimeout(function(){
                // 同步会话列表成功之后，重新获取会话列表
                var ConversationList = RongIMClient.getInstance().getConversationList();
                // do something...
            },1000)
        },onError:function(){
            // 失败
        }
    });
    RongIMClient.getInstance().getHistoryMessages(RongIMClient.ConversationType.PRIVATE,'targeid',20,{
        onSuccess:function(symbol,HistoryMessages){
            // symbol为boolean值，如果为true则表示还有剩余历史消息可拉取，为false的话表示没有剩余历史消息可供拉取。
            // HistoryMessages 为拉取到的历史消息列表，列表中消息为对应消息类型的消息实体
        },onError:function(){
            // APP未开启消息漫游或处理异常
            // throw new ERROR ......
        }
    });*/
    $(function(){
        $("#nav a").click(function (e) {
            $("#nav a").removeClass("on");
            $(e.currentTarget).addClass("on");
            $(".consultTab , .homeTab").addClass("hide");
            $("."+$(e.currentTarget).data("tab")).removeClass("hide");
        })
        var map = {
            monday_a: "周一上午",
            monday_m: "周一下午",
            monday_p: "周一晚上",
            tuesday_a: "周二上午",
            tuesday_m: "周二下午",
            tuesday_p: "周二晚上",
            wednesday_a: "周三上午",
            wednesday_m: "周三下午",
            wednesday_p: "周三晚上",
            thursday_a: "周四上午",
            thursday_m: "周四下午",
            thursday_p: "周四晚上",
            friday_a: "周五上午",
            friday_m: "周五下午",
            friday_p: "周五晚上",
            saturday_a: "周六上午",
            saturday_m: "周六下午",
            saturday_p: "周六晚上",
            sunday_a: "周日上午",
            sunday_m: "周日下午",
            sunday_p: "周日晚上"
        };
        if( $("body").data("hour") ){
            var arr = $("body").data("hour").split(",");
            var str ="";
            for( var i = 0 ; i< arr.length ; i++){
                str += map[ arr[i] ]+","
            }
            str = str.substring(0,str.length-1);
            $("#hour").html(str);
        }else{
            $("#hour").html("暂无门诊时间");
        }

        $(".doctor_ico_del").on("click",function(){
            $(this).parents("li").hide(300);
        })
    })
</script>

</body>
</html>